home *** CD-ROM | disk | FTP | other *** search
- /*
- * scanf regression test
- *
- * usage: tscanf < tscanf.in
- *
- * please add to this
- *
- * ++jrb bammi@cadence.com
- * with parts from glibc
- */
- #include <stdio.h>
- #if __STDC__
- # include <stddef.h>
- # ifndef EXIT_FAILURE
- # include <stdlib.h> /* HP strikes again! */
- # endif
- #else
- # define EXIT_FAILURE (1) /* failure return value for exit() */
- # define EXIT_SUCCESS (0) /* success return value for exit() */
- #endif
-
- int main()
- {
- int i, n;
- float x;
- char name[50];
- int exit_val = EXIT_SUCCESS;
-
- /* input: 25 54.32E-1 thompson */
- /* output: n = 3 i = 25 x = 5.432000 name = :thompson: */
- n = scanf("%d%f%s", &i, &x, name);
- printf("Expect:\tn = 3 i = 25 x = 5.432000 name = :thompson:\n");
- printf("Got:\tn = %d i = %d x = %f name = :%s:\n\n", n, i, x, name);
- if(n != 3) exit_val |= EXIT_FAILURE;
-
- /* input: 56789 0123 56a72 */
- /* output: n = 3 i = 56 x = 789.000000 name = :56: */
- /* a72 left over */
- n = scanf("%2d%f%*d %[0-9]", &i, &x, name);
- printf("Expect:\tn = 3 i = 56 x = 789.000000 name = :56:\n");
- printf("Got:\tn = %d i = %d x = %f name = :%s:\n\n", n, i, x, name);
- if(n != 3) exit_val |= EXIT_FAILURE;
-
- /* output: n = 1 name = :a72: */
- n = scanf("%s", name);
- printf("Expect:\tn = 1 name = :a72:\n");
- printf("Got:\tn = %d name = :%s:\n\n", n, name);
- if(n != 1) exit_val |= EXIT_FAILURE;
-
- /* input: 56789 0123 56a72 */
- /* output: n = 3 i = 56 x = 789.000000 name = :56: */
- /* 'a72' left over */
- n = scanf("%2d%f%*d %[0123456789]", &i, &x, name);
- printf("Expect:\tn = 3 i = 56 x = 789.000000 name = :56:\n");
- printf("Got:\tn = %d i = %d x = %f name = :%s:\n\n", n, i, x, name);
- if(n != 3) exit_val |= EXIT_FAILURE;
-
- /* output: n = 1 name = :a72: */
- n = scanf("%s", name);
- printf("Expect:\tn = 1 name = :a72:\n");
- printf("Got:\tn = %d name = :%s:\n\n", n, name);
- if(n != 1) exit_val |= EXIT_FAILURE;
-
- /* input: 2 quarts of oil
- 10.0LBS of fertilizer
- 100ergs of energy
- output:
- count = 3, quant = 2.000000, item = oil, units = quarts
- count = 3, quant = 10.000000, item = fertilizer, units = LBS
- count = 3, quant = 100.000000, item = energy, units = rgs
- count = -1, quant = 0.000000, item = , units =
- */
- printf("Expect:\n\tcount = 3, quant = 2.000000, item = oil, units = quarts\n");
- printf("\tcount = 3, quant = 10.000000, item = fertilizer, units = LBS\n");
- printf("\tcount = 3, quant = 100.000000, item = energy, units = rgs\n");
- printf("\tcount = -1, quant = 0.000000, item = , units = \n");
- printf("Got:\n");
- while (!feof (stdin) && !ferror (stdin))
- {
- float quant = 0.0;
- char units[21], item[21];
- units[0] = item[0] = '\0';
- n = fscanf (stdin, "%f%20s of %20s", &quant, units, item);
- (void) fscanf (stdin, "%*[^\n]");
- printf ("\tcount = %d, quant = %f, item = %.21s, units = %.21s\n",
- n, quant, item, units);
- }
- if(n != EOF) exit_val |= EXIT_FAILURE;
-
- return exit_val;
- }
-
-